import OSS from "ali-oss"; // 阿里云OSS SDK
export const Upload = (folder, file) => {
  return uploadToOSS(folder, file);
};
const uploadToOSS = (folder, file) => {
  return new Promise((resolve, reject) => {
    const OSS_ACCESS_KEY_ID = "LTAI5tFtkqEu84D8SsnrrmHh";
    const OSS_ACCESS_KEY_SECRET = "eakK8uQ5Us5NayJ7ydKoh05HWl4zVH";
    const OSS_BUCKET_NAME = "qianmaleiao";
    const OSS_REGION = "oss-cn-beijing";

    const ossClient = new OSS({
      accessKeyId: OSS_ACCESS_KEY_ID,
      accessKeySecret: OSS_ACCESS_KEY_SECRET,
      bucket: OSS_BUCKET_NAME,
      region: OSS_REGION,
    });

    const key = `${folder}/${Date.now()}_${file?.name || ""}`;
    console.log(key);
    ossClient.put(key, file).then(
      (result) => {
        resolve(result);
        console.log("文件上传成功：", result);
      },
      (err) => {
        reject(err);
        console.error("文件上传失败：", err);
      }
    );
  });
};
